/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.insurance.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.insurance.domain.InsuredUser;
import me.zhengjie.modules.insurance.pojo.MatchingSiteRequest;
import me.zhengjie.modules.insurance.service.InsuredUserService;
import me.zhengjie.modules.insurance.service.dto.InsuredUserDto;
import me.zhengjie.modules.insurance.service.dto.InsuredUserQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author HeHui
* @date 2021-12-26
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "被保用户管理")
@RequestMapping("/api/insured-user")
public class InsuredUserController {

    private final InsuredUserService insuredUserService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('insured:list')")
    public void download(HttpServletResponse response, InsuredUserQueryCriteria criteria) throws IOException {
        insuredUserService.download(insuredUserService.queryAll(criteria), response);
    }

    @GetMapping
    //@Log("查询被保用户")
    @ApiOperation("查询被保用户")
    @PreAuthorize("@el.check('insured:list')")
    public ResponseEntity<Object> query(InsuredUserQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(insuredUserService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增人员")
    @ApiOperation("新增被保用户")
    @PreAuthorize("@el.check('insured:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody InsuredUser resources){
        return new ResponseEntity<>(insuredUserService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改人员")
    @ApiOperation("修改被保用户")
    @PreAuthorize("@el.check('insured:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody InsuredUser resources){
        insuredUserService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除人员")
    @ApiOperation("删除被保用户")
    @PreAuthorize("@el.check('insured:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        insuredUserService.deleteAll(ids, SecurityUtils.getCurrentUserId());
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Log("减员")
    @ApiOperation("批量减员")
    @PreAuthorize("@el.check('insured:out')")
    @PostMapping("/out")
    public ResponseEntity<Void> batchOut(@RequestBody List<Long> ids) {
        insuredUserService.batchOut(ids, SecurityUtils.getCurrentUserId());
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /**
     * 上传
     *
     * @param file 文件
     * @return {@link ResponseEntity}<{@link Void}>
     */
    @Log("增员导入")
    @PostMapping("/upload")
    @ApiOperation("导入")
    //@PreAuthorize("@el.check('insured:add')")
    @AnonymousAccess
    public ResponseEntity<Void> upload(@RequestParam MultipartFile file) throws IOException {
        insuredUserService.upload(file, 1L);
        return new ResponseEntity<>(HttpStatus.OK);
    }




    /**
     * 匹配所属
     *
     * @param request 请求
     * @return {@link ResponseEntity}<{@link Long}>
     */
    @PostMapping("/matching")
    public ResponseEntity<InsuredUserDto> matching(@RequestBody @Validated MatchingSiteRequest request) {
        return new ResponseEntity<>(insuredUserService.matching(request),HttpStatus.OK);
    }
}